package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.*;
import com.asm.vo.sys.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @author morningbear
 * @date 2019/7/17
 */
@ApiModel("用于处理预定信息的Controller层接口")
@Slf4j
@RestController
public class BookController extends BaseController {

    @Resource
    private BookService bookService;

    @Resource
    private SpotService spotService;

    @Resource
    private WorkOrderService workOrderService;

    /**
     * 分页查询所有预定信息
     *
     * @param pageNumber 页码
     * @param pageSize   页大小
     * @return 预定信息列表
     */
    @ApiOperation("查询所有的预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小")
    })
    @GetMapping("/books")
    public ResponseEntity<PageResult<BookVo>> getAllBooks(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", bookService.findAllBooks(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取预定信息
     *
     * @param id 预定信息编号
     * @return 预定信息
     */
    @ApiOperation("根据id获取预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "预定信息编号")
    })
    @GetMapping("/book/{id}")
    public ResponseEntity<Result<BookVo>> getBookById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", bookService.findBookById(id)), HttpStatus.OK);
    }

    /**
     * 根据顾客id返回预定信息列表
     *
     * @param customerId 顾客id
     * @return 预定信息
     */
    @ApiOperation("根据顾客id获取指定预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "顾客id")
    })
    @GetMapping("/book/getBooks/{customerId}")
    public ResponseEntity<Result> getBooksByCustomerId(@PathVariable("customerId") Long customerId) {
        return new ResponseEntity<>(new Result().ok("success", bookService.findBookByCustomerId(customerId)), HttpStatus.OK);
    }

    /**
     * 根据合同id分页查询预定信息
     *
     * @param contractId 合同id
     * @param pageNumber 页码
     * @param pageSize   页大小
     * @return 预定信息列表
     */
    @ApiOperation("根据合同id分页查询相关预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractId", value = "合同编号"),
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小")
    })
    @GetMapping("/books/contractId/{contractId}")
    public ResponseEntity<PageResult<BookVo>> getBooksBySize(@PathVariable("contractId") Long contractId, @RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", bookService.findBookByContractId(contractId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 生成预定信息预定广告位
     *
     * @param bookVo 预定信息
     * @return 响应信息
     */
    @ApiOperation("生成预定信息预定广告位")
    @PostMapping("/book/bookSpot")
    public ResponseEntity<Result> repairApply(@RequestBody BookVo bookVo) {
        if (spotService.isSpotReserved(bookVo.getCustomerId(), bookVo.getSpotId(), bookVo.getStartDate(), bookVo.getEndDate())) {
            return new ResponseEntity<>(new Result().serverError("该广告位已预订"), HttpStatus.OK);
        }
        bookService.repairApply(bookVo);
        return new ResponseEntity<>(new Result().ok("create book information success"), HttpStatus.OK);
    }

    /**
     * 管理员审批通过，并创建工单
     *
     * @param bookVo 合同信息
     * @return 相应信息
     */
    @ApiOperation("管理员审批通过预定，并创建工单")
    @PostMapping("/book/confirm")
    public ResponseEntity<Result> applyConfirm(@RequestBody @ApiParam(name = "bookVo", value = "预定信息") BookVo bookVo) {
        if (bookVo.getContractId() != 0) {
            return new ResponseEntity<>(new Result().serverError("已经生成合同了"), HttpStatus.OK);
        }
        bookService.applyConfirm(bookVo);
        return new ResponseEntity<>(new Result().ok("审核通过"), HttpStatus.OK);
    }

    /**
     * 随机派遣一名工人去安装广告
     *
     * @param bookVo 预定信息
     * @return 响应信息
     */
    @ApiOperation("派遣一名工人去安装广告")
    @PostMapping("/book/assignTask")
    public ResponseEntity<Result> assignTask(@RequestBody @ApiParam(name = "bookVo", value = "预定信息") BookVo bookVo) {
        if (workOrderService.findWorkOrderById(bookVo.getWorkOrderId()).getWorkerId() != null) {
            return new ResponseEntity<>(new Result().serverError("已经派发任务了"), HttpStatus.OK);
        }
        bookService.assignTask(bookVo);
        return new ResponseEntity<>(new Result().ok("成功派发任务"), HttpStatus.OK);
    }

    /**
     * 管理员撤销预定申请
     *
     * @param bookVo 预定信息
     * @return 响应信息
     */
    @ApiOperation("管理员撤销预定申请")
    @PostMapping("/book/cancel")
    public ResponseEntity<Result> applyCancel(@RequestBody @ApiParam(name = "bookVo", value = "预定信息") BookVo bookVo) {
        bookService.applyCancel(bookVo);
        return new ResponseEntity<>(new Result().ok("审批失败"), HttpStatus.OK);
    }

    /**
     * 增加预定信息记录
     *
     * @param bookVo 预定信息
     * @return 响应信息
     */
    @ApiOperation("增加预定信息记录")
    @PostMapping("/book/add")
    public ResponseEntity<Result> addBook(@RequestBody @ApiParam(name = "bookVo", value = "预定信息") BookVo bookVo) {
        bookService.addBook(bookVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新预定信息
     *
     * @param bookVo 预定信息
     * @return 相应信息
     */
    @ApiOperation("更新预定信息")
    @PutMapping("/book/update")
    public ResponseEntity<Result> updateBook(@RequestBody @ApiParam(name = "bookVo", value = "预定信息") BookVo bookVo) {
        bookService.updateBook(bookVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据Id删除预定信息
     *
     * @param id 预定信息编号
     * @return 响应信息
     */
    @ApiOperation("根据Id删除预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "预定信息id")
    })
    @DeleteMapping("/book/delete/{id}")
    public ResponseEntity<Result> deleteBook(@PathVariable("id") Long id) {
        bookService.deleteBook(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }
}
